package oceanwiki.config;

import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.AuthorizationScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @FileName SwaggerConfig
 * @Description
 * @Author Me
 * @date 2024-09-07
 **/

@Configuration
@EnableSwagger2
public class SwaggerConfig {
   @Bean
   public Docket createRestApi() {
       //1. 创建swagger配置类

       Docket docket = new Docket(DocumentationType.SWAGGER_2);
       ApiInfoBuilder builder = new ApiInfoBuilder();
       builder.title("海洋科普网站后台系统");
       ApiInfo info = builder.build();
       docket.apiInfo(info);

       //2. 设置哪些方法要放在swagger2页面内
       ApiSelectorBuilder apiSelectorBuilder = docket.select();
       //选择所有包下的类
       apiSelectorBuilder.paths(PathSelectors.any());
       //添加ApiOperation注解的方法
       apiSelectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));

       //使Swagger支持JWT
       ApiKey apiKey = new ApiKey("token", "token", "header");
       List<ApiKey> apiKeyList = new ArrayList<>();

       docket.securitySchemes(apiKeyList);

       //4.设置令牌作用域为全局
       AuthorizationScope scope = new AuthorizationScope("glibal", "accessEverything");
       AuthorizationScope[] scopes = {scope};
       SecurityReference reference = new SecurityReference("token", scopes);
       List refList = new ArrayList();
       refList.add(reference);
       SecurityContext context = SecurityContext.builder().securityReferences(refList).build();
       List cxtList = new ArrayList();
       cxtList.add(context);
       docket.securityContexts(cxtList);
       return docket;


   }
}
